//
//  OrderDetailViewController.m
//  shanHaiHuiApp
//
//  Created by DS on 2018/3/16.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "OrderDetailViewController.h"
#import "GoodSuccessTableViewCell.h"
#import "CardTableViewCell.h"
#import "CardHeaderView.h"
#import "MemSelfView.h"
#import "MemDispatchView.h"
#import "StoreDispatchView.h"
#import "PayMentGoodModel.h"
#import "SpphModel.h"
#import "PaysucessTableViewCell.h"
#import "PaySuccessDispatchTableViewCell.h"


@interface OrderDetailViewController ()<UITableViewDelegate,UITableViewDataSource>


Strong UITableView * tableView;

Strong UIView * headerView;


//会员自提头部视图

Strong MemSelfView * memSelfView;
Strong MemDispatchView * memDispatchView;
Strong StoreDispatchView * storeDispatchView;
Strong StoreDispatchView * storeSelfView;


//接收订单
Strong NSMutableDictionary * orderDicSuccess;

//购买的商品
Strong NSMutableArray * orderArrSuccess;

//商品批号
Strong NSMutableArray * orderSpphArrSuccess;


Strong NSMutableArray * combArr;

Strong  NSString * fDatetime;


Strong NSString * disttime;

//商品批号比较

Strong NSString * strGood;
Strong NSString * strSpph;

Strong NSMutableArray * sphArr;

@end

//网络
@implementation OrderDetailViewController (NetWorking)

- (void)getOrderDetaileData{
    
    [self showprogressHUD];
    NSDictionary *parameters = @{
                                 
                                 @"orderNo":self.orderNo
                                 
                                 }.mutableCopy;
    
    [[AFAppDotNetAPIClient sharedClient] POST:@"morderPage.do" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        
        if ([responseObject[@"code"] intValue] == 0) {
            
            NSLog(@"responseObject === %@",responseObject);
            
            self.orderDicSuccess = responseObject[@"order"];
         
            for (NSDictionary * dic in responseObject[@"detailList"]) {
                PayMentGoodModel * model = [[PayMentGoodModel alloc] init];
                [model payMentGoodModelWithDic:dic];
                [self.orderArrSuccess addObject:model];
            }
            
            for (NSDictionary * dic in responseObject[@"spphList"]) {
                
                SpphModel * model = [[SpphModel alloc] init];
                [model spphModelWithDic:dic];
                [self.orderSpphArrSuccess addObject:model];
            }
            
            
            for (PayMentGoodModel * model in self.orderArrSuccess) {
                NSString *natureid = model.spbh;
                NSMutableArray *arr = [NSMutableArray arrayWithCapacity:5];
                for (SpphModel * model in self.orderSpphArrSuccess) {
                    NSString *natureid1 = model.fSpbh;
                    if ([natureid1 isEqualToString:natureid]) {
                        [arr addObject:model];
                    }
                }
                [self.combArr addObject:arr];
            }
     
            [self.view addSubview:self.tableView];
            
            
        }else{
            ShowMessage(responseObject[@"msg"]);
            
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}


//成功接口
- (void)comPeleteData{
    [self showprogressHUD];
    NSDictionary *parameters = @{
                                 @"orderNo":self.orderNo,
                                 };
    [[AFAppDotNetAPIClient sharedClient] POST:@"morderDistFinish.do" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        
        
        if ([responseObject[@"code"] intValue] == 0) {
    
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}


@end


@implementation OrderDetailViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self NavColor];
    
    [self getOrderDetaileData];

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"订单详情";
 
}

// 导航颜色
- (void)NavColor{
    
    UINavigationBar * bar = self.navigationController.navigationBar;
    UIImage *bgImage = [UIImage imageWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64) WihtColor:getColor(navColor) alphe:1.0];
    [bar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
    
    
    [bar setShadowImage:[UIImage imageWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0.1) WihtColor:[UIColor blackColor] alphe:1.0]];
    
}


#pragma init --

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - height64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.bounces = NO;
    }
    return _tableView;
}



#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    

    
    if (section == 4) {
        
        return self.orderArrSuccess.count;
        
    }else{
        
        return 1;
    }
    
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    

    return 14;
    
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        PaySuccessDispatchTypeTableViewCell * cell = [PaySuccessDispatchTypeTableViewCell  cellForTableView:tableView];
        
        if ([self.disPatchStyle isEqualToString:@"mem1"]) {
            
            cell.dispatchTypeLabel.text = [NSString stringWithFormat:@"配送方式:%@",@"会员自提"];
            
        }else if ([self.disPatchStyle isEqualToString:@"mem0"]){
            
             cell.dispatchTypeLabel.text = [NSString stringWithFormat:@"配送方式:%@",@"会员配送"];
            
        }else if ([self.disPatchStyle isEqualToString:@"org0"]){
            
            cell.dispatchTypeLabel.text = [NSString stringWithFormat:@"配送方式:%@",@"门店配送"];
            
        }else if ([self.disPatchStyle isEqualToString:@"org1"]){
            
            cell.dispatchTypeLabel.text = [NSString stringWithFormat:@"配送方式:%@",@"门店自提"];
            
        }
     
        return cell;
        
        
    } else if (indexPath.section == 1) {
        
        if ([self.disPatchStyle isEqualToString:@"org0"]){
            
            PaySuccessDispatchTypeTableViewCell * cell = [PaySuccessDispatchTypeTableViewCell  cellForTableView:tableView];
            
            cell.dispatchTypeLabel.text = [NSString stringWithFormat:@"配送门店:%@",self.orderDicSuccess[@"orgname"]];
            
            return cell;
            
        }else if ([self.disPatchStyle isEqualToString:@"org1"]){
            
           PaySuccessDispatchTypeTableViewCell * cell = [PaySuccessDispatchTypeTableViewCell  cellForTableView:tableView];
            
            cell.dispatchTypeLabel.text = [NSString stringWithFormat:@"配送门店:%@",self.orderDicSuccess[@"orgname"]];
            
            return cell;
            
        }else if ([self.disPatchStyle isEqualToString:@"mem0"]){
        
            PaySuccessDispatchAdressTableViewCell * cell = [PaySuccessDispatchAdressTableViewCell cellForTableView:tableView];
            
            
            cell.goodLabel.text = @"收货人";
            
            cell.phoneLabel.text = self.orderDicSuccess[@"distconsigneephone"];
            
            cell.adressLabel.text = self.orderDicSuccess[@"distaddress"];
            
            cell.nameLabel.text = self.orderDicSuccess[@"distconsignee"];
            
        
             return cell;
            
        }else if ([self.disPatchStyle isEqualToString:@"mem1"]){
            
            PaySuccessDispatchAdressTableViewCell * cell = [PaySuccessDispatchAdressTableViewCell cellForTableView:tableView];
            
            cell.hidden = YES;
            
            
             return cell;
            
        }
        
        
        return nil;
        
    } else if (indexPath.section == 2) {
        
        PaySuccessDispatchRemarkTableViewCell * cell = [PaySuccessDispatchRemarkTableViewCell cellForTableView:tableView];
        
        if ([self.disPatchStyle isEqualToString:@"mem1"] || self.orderDicSuccess[@"distremark"] == 0) {
            
            cell.hidden = YES;
            
        }else{
            
            NSString * disMark = self.orderDicSuccess[@"distremark"];
            
            if (disMark.length > 0) {
                
                cell.remarkLabel.text = @"配送备注:";
                
                cell.remarkContentLabel.text = self.orderDicSuccess[@"distremark"];
                
            }else{
                
                cell.hidden = YES;
            }
 
            
        }
  
        return cell;
        
    } else if (indexPath.section == 3) {
        
         PaySuccessDispatchPersonTableViewCell * cell = [PaySuccessDispatchPersonTableViewCell cellForTableView:tableView];
        
        
        if ([self.disPatchStyle isEqualToString:@"mem1"]) {
            
            cell.hidden = YES;
     
        }else{
            
            if ([self.orderDicSuccess[@"diststatus"] isEqualToString:@"1"]) {
                
                cell.personLabel.hidden = NO;
                
                cell.timeLabel.hidden = NO;
                
                cell.configBtn.hidden = YES;
                
                cell.personLabel.text = [NSString stringWithFormat:@"配送人员:%@",self.orderDicSuccess[@"distuserid"]];
                
                
                NSDictionary * dic1 = self.orderDicSuccess[@"fDatetime"];
                
                NSString *str1 = [NSString stringWithFormat:@"%ld",([dic1[@"time"] integerValue] / 1000)];
                
                NSString * timeStr = [NSString stringWithFormat:@"%@",[self timeWithTimeIntervalString1:str1]];
                
                cell.timeLabel.text = [NSString stringWithFormat:@"配送时间:%@",timeStr];
                
                
                
          
            }else{
                
                cell.personLabel.hidden = YES;
                
                cell.timeLabel.hidden = YES;
                
                cell.configBtn.hidden = NO;
                
            }
            
            // diststatus
            
            cell.getConFigClick = ^(PaySuccessDispatchPersonTableViewCell *cell) {
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"是否确认配送"
                                                                               message:nil
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                          
                                                                      }];
                UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive
                                                                     handler:^(UIAlertAction * action) {
                                                                         
                                                                         [self comPeleteData];
                                                                         
                                                                         cell.personLabel.hidden = NO;
                                                                         
                                                                         cell.timeLabel.hidden = NO;
                                                                         
                                                                         cell.configBtn.hidden = YES;
                                                                         
                                                                         cell.personLabel.text = [NSString stringWithFormat:@"配送人员:%@",self.orderDicSuccess[@"distuserid"]];
                                                                         
                                                                         
                                                                         NSDictionary * dic1 = self.orderDicSuccess[@"fDatetime"];
                                                                         
                                                                         NSString *str1 = [NSString stringWithFormat:@"%ld",([dic1[@"time"] integerValue] / 1000)];
                                                                         
                                                                         NSString * timeStr = [NSString stringWithFormat:@"%@",[self timeWithTimeIntervalString1:str1]];
                                                                         
                                                                         cell.timeLabel.text = [NSString stringWithFormat:@"配送时间:%@",timeStr];
                                                                         
                                                                         
                                                                         
                                                                     }];
                
                [alert addAction:defaultAction];
                [alert addAction:cancelAction];
                [self presentViewController:alert animated:YES completion:nil];
                
               
                
            };
            
            
        }
    
        return cell;
    
    }else if(indexPath.section == 4){
        
        PaysucessGoodTableViewCell * cell = [PaysucessGoodTableViewCell cellForTableView:tableView];
        
        cell.model = self.orderArrSuccess[indexPath.row];
        
        
        
        cell.sphArr = self.combArr[indexPath.row];
        
        self.sphArr = self.combArr[indexPath.row];
    
        return cell;
        
        
        
    }else if (indexPath.section == 5){
        
        PaysucessTotalTableViewCell * cell = [PaysucessTotalTableViewCell cellForTableView:tableView];
        
        cell.orderNo.text = [NSString stringWithFormat:@"单号:%@",self.orderDicSuccess[@"orderNo"]];
        
        cell.totalMoney.text = [NSString stringWithFormat:@"总计:￥%@",self.orderDicSuccess[@"orderXsje"]];
        
        return cell;
        
        
    }else if (indexPath.section == 6){
        
        PaysucessCashTableViewCell * cell = [PaysucessCashTableViewCell cellForTableView:tableView];
        
        if ([self.orderDicSuccess[@"orderCardPay"] floatValue] == 0) {
            
            cell.hidden = YES;
            
            
            
        }else{
            
            cell.cashLabel.text = @"会员卡支付";
            
            cell.moneyLabel.text = [NSString stringWithFormat:@"￥%@",self.orderDicSuccess[@"orderCardPay"]];
            
        }
        
        
        return cell;
        
    }else if (indexPath.section == 7){
        
        PaysucessCashTableViewCell * cell = [PaysucessCashTableViewCell cellForTableView:tableView];
        
        if ([self.orderDicSuccess[@"orderCardBalance"] floatValue] == 0) {
            
            
            cell.hidden = YES;
            
            
            
        }else{
            
            cell.cashLabel.text = @"支付后余额";
            
            cell.cashImageView.image = [UIImage imageNamed:@"yue"];
            
            cell.moneyLabel.text = [NSString stringWithFormat:@"￥%@",self.orderDicSuccess[@"orderCardBalance"]];
            
            
        }
        
        
        return cell;
        
    }else if (indexPath.section == 8){
        
        PaysucessCashTableViewCell * cell = [PaysucessCashTableViewCell cellForTableView:tableView];
        
        if ([self.orderDicSuccess[@"orderCashAll"] floatValue] == 0) {
            
            cell.hidden = YES;
            
            
        }else{
            
            cell.cashLabel.text = @"现金支付";
            
            cell.cashImageView.image = [UIImage imageNamed:@"xianjin"];
            
            cell.moneyLabel.text = [NSString stringWithFormat:@"￥%@",self.orderDicSuccess[@"orderCashAll"]];
            
            
        }
        
        
        
        return cell;
        
    }else if (indexPath.section == 9){
        
        PaysucessCashTableViewCell * cell = [PaysucessCashTableViewCell cellForTableView:tableView];
        
        if ([self.orderDicSuccess[@"orderCashChange"] floatValue] == 0) {
            
            cell.hidden = YES;
            
        }else{
            
            cell.cashLabel.text = @"找零";
            
            cell.cashImageView.image = [UIImage imageNamed:@"zhaoling"];
            
            cell.moneyLabel.text = [NSString stringWithFormat:@"￥%@",self.orderDicSuccess[@"orderCashChange"]];
            
            cell.lineView.backgroundColor = getColor(@"F0F0F0");
            
            [cell.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(UNIT_HEIGHT(5));
            }];
            
        }
        
        return cell;
        
    }else if (indexPath.section == 10){
        
        PaysucessCashTableViewCell * cell = [PaysucessCashTableViewCell cellForTableView:tableView];
      
        cell.cashLabel.text = [NSString stringWithFormat:@"卡号:%@",self.orderDicSuccess[@"fCardno"]];
        
        cell.moneyLabel.text = [NSString stringWithFormat:@"%@",self.orderDicSuccess[@"fName"]];
        
        cell.cashImageView.image = [UIImage imageNamed:@"huiyuanka拷贝"];
   
        return cell;
        
    }else if (indexPath.section == 11){
        
        PaysucessCashTableViewCell * cell = [PaysucessCashTableViewCell cellForTableView:tableView];
        
        cell.cashLabel.text = @"手机号";
        
        cell.cashImageView.image = [UIImage imageNamed:@"shouji"];
        
        cell.moneyLabel.text = self.orderDicSuccess[@"fMobiletel"];
        
        return cell;
        
    }else if (indexPath.section == 12){
        
        PaysucessCashTableViewCell * cell = [PaysucessCashTableViewCell cellForTableView:tableView];
        
        cell.cashLabel.text = [UserModel sharedInstanced].userID;
        
        NSDictionary * dic1 = self.orderDicSuccess[@"fDatetime"];
        
        NSString *str1 = [NSString stringWithFormat:@"%ld",([dic1[@"time"] integerValue] / 1000)];
        
        NSString * timeStr = [NSString stringWithFormat:@"%@",[self timeWithTimeIntervalString1:str1]];
    
        cell.moneyLabel.text = timeStr;
        
        cell.cashImageView.image = [UIImage imageNamed:@"yiingyeyuan"];
        
        return cell;
        
    }else{
        
        PaysucessPlaceTableViewCell * cell = [PaysucessPlaceTableViewCell cellForTableView:tableView];
        
        cell.moneyLabel.text = self.orderDicSuccess[@"orgname"];
        
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {

        return UNIT_HEIGHT(50);
        
        
    }else if (indexPath.section == 1){
        
        if ([self.disPatchStyle isEqualToString:@"org0"]){
            
            return UNIT_HEIGHT(50);
            
        }else if ([self.disPatchStyle isEqualToString:@"org1"]){
            
            return UNIT_HEIGHT(50);
            
        }else if ([self.disPatchStyle isEqualToString:@"mem0"]){
            
            return UNIT_HEIGHT(80);
            
        }else{
            
            return 0;
            
        }
        
    }else if(indexPath.section == 2){
        
        if ([self.disPatchStyle isEqualToString:@"mem1"] || self.orderDicSuccess[@"distremark"] == 0) {
            
            return UNIT_HEIGHT(0);
            
        }else{
            
            NSString * disMark = self.orderDicSuccess[@"distremark"];
            
            if (disMark.length > 0) {
                
                return UNIT_HEIGHT(50);
                
            }else{
                
                    return 0;
            }
        
            
            
        }
        
    }else if(indexPath.section == 3){
        
         if ([self.disPatchStyle isEqualToString:@"mem1"]) {
             
             return 0;
             
         }else{
        
             return UNIT_HEIGHT(50);
         }
        
    }else if (indexPath.section == 4) {
        
        return UNIT_HEIGHT(100) + self.sphArr.count  * UNIT_HEIGHT(130);
    }else if(indexPath.section == 6){
        
        if ([self.orderDicSuccess[@"orderCardPay"] floatValue] == 0) {
            
            return 0;
            
        }else{
            
            return UNIT_HEIGHT(50);
            
        }
        
        
    }else if(indexPath.section == 7){
        
        if ([self.orderDicSuccess[@"orderCardBalance"] floatValue] == 0) {
            
            return 0;
            
        }else{
            
            return UNIT_HEIGHT(50);
            
        }
        
        
    }else if(indexPath.section == 8){
        
        if ([self.orderDicSuccess[@"orderCashAll"] floatValue] == 0) {
            
            return 0;
            
        }else{
            
            return UNIT_HEIGHT(50);
            
        }
        
        
    }else if(indexPath.section == 9){
        
        if ([self.orderDicSuccess[@"orderCashChange"] floatValue] == 0) {
            
            
            return 0;
            
        }else{
            
            return UNIT_HEIGHT(50);
            
        }
    
    
    
    
    } else{
        
        return UNIT_HEIGHT(50);
        
    }
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

#pragma mark ---

- (NSMutableDictionary *)orderDicSuccess{
    
    if (!_orderDicSuccess) {
        _orderDicSuccess = [NSMutableDictionary dictionary];
    }
    return _orderDicSuccess;
}

- (NSMutableArray *)orderArrSuccess{
    
    if (!_orderArrSuccess) {
        
        _orderArrSuccess = @[
                             
                             ].mutableCopy;
    }
    return _orderArrSuccess;
}

- (NSMutableArray *)orderSpphArrSuccess{
    
    if (!_orderSpphArrSuccess) {
        
        _orderSpphArrSuccess = @[
                                 
                                 ].mutableCopy;
        
    }
    return _orderSpphArrSuccess;
}

- (NSMutableArray *)combArr{
    
    if (!_combArr) {
        
        _combArr = @[].mutableCopy;
    }
    
    return _combArr;
    
}


- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"beijing"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy.MM.dd"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

- (NSString *)timeWithTimeIntervalString1:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"beijing"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy.MM.dd  HH:mm"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

- (NSMutableArray *)sphArr{
    
    
    if (!_sphArr) {
        
        _sphArr = @[].mutableCopy;
    }
    return _sphArr;
}


@end
